﻿using ConsoleAppEFcoreTest2.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppEFcoreTest2.LocalContext
{
    public class MyDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);

        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            //添加mysql驱动
            var connectionString = "server=127.0.0.1,3306;user=root;password=abc123;database=ef";
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 23));
            optionsBuilder.UseMySql(connectionString, serverVersion);
            //在传统的api 里边也可以使用DI依赖注入进行添加数据库驱动，具体可以看Nuget上的github项目的示例

            optionsBuilder.LogTo(Console.Write);
        }

        public DbSet<House> Houses { get; set; }
    }
}
